﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using BlueDot.Data.Core;
using BlueDot.Data.Core.Sql;
using SaveRetrieveData.Data.Entities;

namespace SaveRetrieveData
{
    internal class Program
    {
        #region private

        private static void BasicCRUD()
        {
            //create new user
            var user = new User {FirstName = "Seumas", LastName = "VanMatre"};

            //save the new user
            user.Save();

            //update user
            user.FirstName = "SirBarksAlot";

            //save user
            user.Save();

            //mark the user as deleted
            user.Delete();

            user.Save();

            User deletedUser = DataPersistence.Select<User>(new {user.UserID}).SingleOrDefault();

            //notice user is now null
            Console.WriteLine(deletedUser == null ? "Deleted" : "OOOPS :(");
        }

        private static void BasicSelect()
        {
            var user = new User {FirstName = "Seumas", LastName = "VanMatre"};

            user.Save();

            User freshUser = DataPersistence.Select<User>(new {user.UserID, FirstName = "Seumas"}).SingleOrDefault();
        }

        private static void CreateDatabase()
        {
            Console.WriteLine("Create SqlCe Provider");
            ProviderFactory.DefaultProvider = new SqlCeProvider
                                                  {
                                                      ConnectionString = "Data Source = mfLYQuickStart.sdf"
                                                  };

            ProviderFactory.DefaultProvider.Log = Console.Out;
            DataStoreSchemaBuilder.CreateDataStore(true);
        }

        private static void LoadTestData()
        {
            var file = new FileStream("data.xml", FileMode.OpenOrCreate);

            var x = new XmlSerializer(typeof (List<User>));
            var newUsers = (List<User>) x.Deserialize(file);

            foreach (User user in newUsers)
            {
                user.Save();
            }
        }

        private static void Main(string[] args)
        {
            //Create are initial datastore
            CreateDatabase();

            //Execute basic CRUD functions
            BasicCRUD();

            //Load test data for Select examples
            LoadTestData();

            //Retrieve loaded data
            RetrieveData();

            Console.ReadLine();
        }

        private static void RetrieveData()
        {
            User user = DataPersistence.Select<User>(new {FirstName = "Seumas"}, null).SingleOrDefault();

            Console.WriteLine("Retrieved UserID:{0}, FirstName:{1}, LastName:{2}",
                              user.UserID, user.FirstName, user.LastName);

            user = DataPersistence.Select<User>(new {FirstName = "Who"}).SingleOrDefault();

            Console.WriteLine(user == null ? "Not found" : "shouldn't of found anyone :(");

            List<User> users = (List<User>)DataPersistence.Select<User>(new {LastName = "VanMatre"});

            Console.WriteLine("Found {0} users with LastName=VanMatre", users.Count);

            foreach (User foundUser in users)
            {
                Console.WriteLine("Retrieved UserID:{0}, FirstName:{1}, LastName:{2}",
                                  foundUser.UserID, foundUser.FirstName, foundUser.LastName);
            }

            users = (List<User>)
                DataPersistence.Select<User>(new {LastName = "VanMatre", IsActive = true});

            Console.WriteLine("Found {0} users with LastName=VanMatre, IsActive=true", users.Count);

            foreach (User foundUser in users)
            {
                Console.WriteLine("Retrieved UserID:{0}, FirstName:{1}, LastName:{2}, IsActive:{3}",
                                  foundUser.UserID, foundUser.FirstName, foundUser.LastName, foundUser.IsActive);
            }
        }

        #endregion
    }
}